package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AdminUser;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping( "/table")
    public TableData table(Customer customer, Integer page, Integer limit) {
        PageInfo<Customer> pageInfo = customerService.getCustomerList(customer, page, limit);
        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping( "/tree")
    public List<Customer> tree() {
        return customerService.getCustomerTree();
    }

    @RequestMapping( "/add")
    public AjaxMessage add(Customer customer) {
        try{
            customerService.addCustomer(customer);
            return new AjaxMessage(true,"添加成功");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false,"添加失败");
    }


    @RequestMapping( "/update")
    public AjaxMessage update(Customer customer) {
        try{
            customerService.updateCustomer(customer);
            return new AjaxMessage(true,"修改成功");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false,"修改失败");
    }

    @RequestMapping( "/del")
    public AjaxMessage del(Integer[] ids) {
        try{
            customerService.deleteCustomer(ids);
            return new AjaxMessage(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false,"删除失败");

    }



}
